﻿using System;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using System.ComponentModel;
using XamlCast.Player.Model;
using System.Collections.ObjectModel;

namespace XamlCast.Player.ViewModels {
    public class MainPageViewModel : INotifyPropertyChanged {
        public static MainPageViewModel CurrentMainPage { get; private set; }

        public MainPageViewModel() {
            CurrentMainPage = this;
            var feed = new PodcastFeed("http://feeds.feedburner.com/xamlcast?format=xml");
            FeedItems = feed.Items;
            FeedItems.CollectionChanged += FeedItems_CollectionChanged;

            //http://search.twitter.com/search.rss?q=xamlcast&rpp=100
        }

        void FeedItems_CollectionChanged(object sender, System.Collections.Specialized.NotifyCollectionChangedEventArgs e) {
            if (FeedItems.Count > 0) {
                FeedItems.CollectionChanged -= FeedItems_CollectionChanged;
                SelectedPodcast = FeedItems[0];
            }

        }

        private string siteTitle;
        public string SiteTitle {
            get {
                return this.siteTitle;
            }
            set {
                if (this.siteTitle != value) {
                    this.siteTitle = value;
                    OnPropertyChanged("SiteTitle");
                }
            }
        }

        public ObservableCollection<PodcastItem> FeedItems { get; private set; }

        private PodcastItem selectedPodcast = null;
        public PodcastItem SelectedPodcast {
            get { return this.selectedPodcast; }
            set {
                if (this.selectedPodcast != value) {
                    this.selectedPodcast = value;
                    OnPropertyChanged("SelectedPodcast");
                }
            }
        }


        #region INotifyPropertyChanged Members

        public event PropertyChangedEventHandler PropertyChanged;

        private void OnPropertyChanged(string propertyName) {
            var temp = PropertyChanged;
            if (temp != null && !string.IsNullOrEmpty(propertyName)) {
                temp(this, new PropertyChangedEventArgs(propertyName));
            }
        }

        #endregion
    }
}
